home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 May / PCPlus May 1998=disk A.iso / full / CBUILDER / SAMS / SAMPLES / CHAP04 / AIRPORT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-12  |  3.0 KB  |  119 lines

  1. //---------------------------------------------------------------------------
  2. #include <condefs.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <iostream.h>
  6. #include <conio.h>
  7. #pragma hdrstop
  8.  
  9. USEUNIT("airplane.cpp");
  10. USERES("Airport.res");
  11. //---------------------------------------------------------------------------
  12. #include "airplane.h"
  13.  
  14. int getInput(int max);
  15. void getItems(int& speed, int& dir, int& alt);
  16.  
  17. int main(int argc, char **argv)
  18. {
  19.     char returnMsg[100];
  20.   //
  21.     // Set up an array of  Airplanes and create
  22.   // three Airplane objects.
  23.   //
  24.     Airplane* planes[3];
  25.   planes[0] = new Airplane("TWA 1040");
  26.   planes[1] = new Airplane("United Express 749", COMMUTER);
  27.   planes[2] = new Airplane("Cessna 3238T", PRIVATE);
  28.   //
  29.   // Start the loop.
  30.   //
  31.   do {
  32.     int plane, message, speed, altitude, direction;
  33.     speed = altitude = direction = -1;
  34.     //
  35.     // Get a plane to whom a message will be sent.
  36.     // List all of the planes and let the user pick one.
  37.     //
  38.       cout << endl << "Who do you want to send a message to?";
  39.     cout << endl << endl << "0. Quit" <<  endl;
  40.     for (int i=0;i<3;i++)
  41.         cout << i + 1 << ". " << planes[i]->name << endl;
  42.     //
  43.     // Call the getInput() function to get the plane number.
  44.     //
  45.       plane = getInput(4);
  46.     //
  47.     // If the user chose item 0 then break out of the loop.
  48.     //
  49.     if (plane == -1) break;
  50.     //
  51.     // The plane acknowledges.
  52.     //
  53.     cout << endl << planes[plane]->name << ", roger.";
  54.     cout << endl << endl;
  55.     //
  56.     // Allow the user to choose a message to send.
  57.     //
  58.       cout << "What message do you want to send?" << endl;
  59.     cout << endl << "0. Quit" << endl;;
  60.     cout << "1. State Change" << endl;
  61.     cout << "2. Take Off" << endl;
  62.     cout << "3. Land" << endl;
  63.     cout << "4. Report Status" << endl;
  64.     message = getInput(5);
  65.     //
  66.     // Break out of the loop if the user chose 0.
  67.     //
  68.     if (message == -1) break;
  69.     //
  70.     // If the user chose item 1 then we need to get input
  71.     // for the new speed, direction, and altitude. Call
  72.     // the getItems() function to do that.
  73.     //
  74.     if (message == 0)
  75.         getItems(speed, direction, altitude);
  76.     //
  77.     // Send the plane the message.
  78.     //
  79.     bool goodMsg = planes[plane]->SendMessage(
  80.         message, returnMsg, speed, direction, altitude);
  81.     //
  82.     // Something was wrong with the message
  83.     //
  84.     if (!goodMsg) cout << endl << "Unable to comply.";
  85.     //
  86.     // Display the plane's response.
  87.     //
  88.       cout << endl << returnMsg << endl;
  89.   } while (1);
  90.   //
  91.   // Delete the Airplaine objects.
  92.   //
  93.   for (int i=0;i<3;i++) delete planes[i];
  94. }
  95.  
  96. int getInput(int max)
  97. {
  98.     int choice;
  99.     do {
  100.       choice = getch();
  101.     choice -= 49;
  102.   } while (choice < -1 || choice > max);
  103.   return choice;
  104. }
  105.  
  106. void getItems(int& speed, int& dir, int& alt)
  107. {
  108.     cout << endl << "Enter new speed: ";
  109.   getch();
  110.   cin >> speed;
  111.   cout << "Enter new heading: ";
  112.   cin >> dir;
  113.   cout << "Enter new altitude: ";
  114.   cin >> alt;
  115.   cout << endl;
  116. }
  117.  
  118.  
  119.